package com.tqjc.execute.utils;

import com.tqjc.area.entity.SteelPlate;
import com.tqjc.execute.task.entity.CurTaskInfo;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.entity.Test;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
public class StaticMethod {

    //钢板号十月份转换标识
    public static String SPECIAL_STEEL_NO_OCT;
    //钢板号十一月份转换标识
    public static String SPECIAL_STEEL_NO_NOV;
    //钢板号十二月份转换标识
    public static String SPECIAL_STEEL_NO_DEC;


    public static void SteelPlate2Test(SteelPlate item1, Test item2){
        item2.setMatNo(item1.getSteelId());
        item2.setMatActLen((float)item1.getLength());
        item2.setMatActWidth((float)item1.getWidth());
        item2.setMatActThick((float)item1.getThickness());
        item2.setMatActWt((float)item1.getWeight());
    }
    public static String updateSteelNo(String crane,String steelNo){
        if("11C南".equals(crane)||"11C北".equals(crane)) {
            if (StringUtils.isNotEmpty(steelNo) && steelNo.length() == 7) {
                steelNo = "0" + steelNo;
            }
            if (StringUtils.isNotEmpty(steelNo) && steelNo.length() == 8) {
                log.info("{}天车系统修改了钢板号{}，尾数变成了100", crane, steelNo);
                return steelNo.substring(0, 5) + "100";
            }
        }

        return steelNo;
    }


    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        StaticMethod.updateSteelNo("测试","59110",true);

    }

    public static String updateSteelNo(String crane,String steelNo,boolean flag){

        //flag判断是否需要转换程序
        if(flag){
            //3/4/5号横移才会进来
            String substring = steelNo.substring(0, 2);
            Integer start = Integer.valueOf(substring.substring(0, 1));
            if(0 == start){
                start = 9;
            } else {
                start = start - 1;
            }
            if(SPECIAL_STEEL_NO_DEC.equals(substring)){
                steelNo = start + "C" + steelNo.substring(2);
                log.info("{}天车系统根据十二月份标识转换钢板号为:{}，前两位：{}更改", crane, steelNo,substring);
                return steelNo;
            }
            if(SPECIAL_STEEL_NO_NOV.equals(substring)){
                steelNo =  start + "B" + steelNo.substring(2);
                log.info("{}天车系统根据十一月份标识转换钢板号为:{}，前两位：{}更改", crane, steelNo,substring);
                return steelNo;
            }
            if(SPECIAL_STEEL_NO_OCT.equals(substring)){
                steelNo =  start + "A" + steelNo.substring(2);
                log.info("{}天车系统根据十月份标识转换钢板号为:{}，前两位：{}更改", crane, steelNo,substring);
                return steelNo;
            }

        }

        if("11C南".equals(crane)||"11C北".equals(crane)) {
            if (StringUtils.isNotEmpty(steelNo) && steelNo.length() == 7) {
                steelNo = "0" + steelNo;
            }
            if (StringUtils.isNotEmpty(steelNo) && steelNo.length() == 8) {
                log.info("{}天车系统修改了钢板号{}，尾数变成了100", crane, steelNo);
                return steelNo.substring(0, 5) + "100";
            }
        }

        return steelNo;
    }







    public static String autoSelectCurMonthSteelNo(List<SteelInfo> steelList,String steelNo,String crane){   //12月份是+24C,11月份：24B
        if("11C南".equals(crane)||"11C北".equals(crane)){
            LocalDateTime now=LocalDateTime.now();
            String month="";
            int monthInt=now.getMonthValue();
            if(monthInt<=9){
                month=monthInt+"";
            }else if(monthInt==10){
                month="A";
            }else if(monthInt==11) {
                month="B";
            }else {
                month="C";
            }
            String year=now.getYear()+"";
            String newSteelNo= year.substring(2)+month+steelNo;
            for (SteelInfo item:steelList){
                if(item.getMatNo().contains(newSteelNo)){
                    return newSteelNo;
                }
            }
        }
        return steelNo;
    }


    public static String getYearAndMonthJoin(){   //12月份是+24C,11月份：24B
        LocalDateTime now=LocalDateTime.now();
        String month="";
        int monthInt=now.getMonthValue();
        if(monthInt<=9){
            month=monthInt+"";
        }else if(monthInt==10){
            month="A";
        }else if(monthInt==11) {
            month="B";
        }else {
            month="C";
        }
        String year=now.getYear()+"";
        String comStr= year.substring(2)+month;
        return comStr;
    }

    public static void writeTaskIdAndSteelType2File(String craneType,String taskId,String steelType){
        String data=taskId+"#"+steelType;
        try {
            File dir=new File("d:/dontDelete");
            if(!dir.exists()){
                dir.mkdir();
            }
            File file = new File("d:/dontDelete/file"+craneType+".txt");
            FileWriter writer = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(data);
            bufferedWriter.close();
            System.out.println("数据写入成功！");
        } catch (Exception e) {
            System.out.println("数据写入失败！");
            e.printStackTrace();
        }
    }

    public static CurTaskInfo readTaskIdAndSteelTypeFromFile(String craneType){
        String filename = "d:/dontDelete/file"+craneType+".txt";

        try {
            FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line=bufferedReader.readLine();
            bufferedReader.close();
            if(line!=null){
                System.out.println(line);
                String[] arr=line.trim().split("#");
                CurTaskInfo info=new CurTaskInfo();
                info.setTaskId(arr[0]);
                info.setSteelType(arr[1]);
                return info;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 检验钢板信息尺寸长度是否合格
     * @param steelPlate   true：表示合格，false表示不合格
     * @return
     */
    public static Boolean checkSteelInfo(SteelPlate steelPlate){
        if(steelPlate == null || steelPlate.getLength() == null || steelPlate.getWidth() == null || steelPlate.getThickness() == null){
            log.error("钢板号：{}，尺寸信息不能存在有空数值",steelPlate.getSteelId());
            return false;
        }

        if(steelPlate.getLength() < 4000 || steelPlate.getLength() > 17000){
            log.error("钢板号：{}，长度检验不通过。信息库中的钢板长度尺寸为：{}",steelPlate.getSteelId(),steelPlate.getLength());
            return false;
        }

        if(steelPlate.getWidth() < 1100 || steelPlate.getWidth() > 3600){
            log.error("钢板号：{}，宽度检验不通过。信息库中的钢板宽度尺寸为：{}",steelPlate.getSteelId(),steelPlate.getWidth());

            return false;
        }

        if(steelPlate.getThickness() < 8 || steelPlate.getThickness() > 100){
            log.error("钢板号：{}，厚度检验不通过。信息库中的钢板厚度尺寸为：{}",steelPlate.getSteelId(),steelPlate.getThickness());

            return false;
        }

        return true;

    }


    public static String getSpecialSteelNoFlagLabel(String craneType){
        if("5#横移".equals(craneType)){
            return "5号横移特殊钢板号标识";
        }
        if("4#横移".equals(craneType)){
            return "4号横移特殊钢板号标识";
        }
        if("3#横移".equals(craneType)){
            return "3号横移特殊钢板号标识";
        }
        return null;
    }



}
